﻿using System.Windows;
using System.Xml;
using AttachedCommands.ACB;
using Microsoft.Win32;
using Snippet2Vsi.Classes;
using Snippet2Vsi.UserControls;

namespace Snippet2Vsi.ViewModels
{
    public class SnippetEditorViewModel:ViewModelBase
    {
        private readonly SnippetEditorView _view;
        public SnippetEditorViewModel(SnippetEditorView view)
        {
            _view = view;
        }

        #region Members

        private RelayCommand _browseVsCommand;
        private RelayCommand _saveCommand;
        private string _snippetPath;

        #endregion

        #region Commands

        public RelayCommand BrowseVsCommand
        {
            get { return _browseVsCommand ?? (_browseVsCommand = new RelayCommand(BrowseVsCommandHandler)); }
            set { _browseVsCommand = value; }
        }



     

        public RelayCommand SaveCommand
        {
            get { return _saveCommand ?? (_saveCommand = new RelayCommand(SaveCommandHandler, CanExecuteSave)); }
            set { _saveCommand = value; }
        }

        private bool CanExecuteSave(object obj)
        {
            return !string.IsNullOrEmpty(SnippetPath);
        }

        #endregion

        #region Handlers

        private void SaveCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(() =>
            {
               if(CanExecuteSave(null))
               {
                   var xmlDoc = new XmlDocument();
                  xmlDoc.Load(SnippetPath);
                   
                   var editot=new XmlDocument();
                   editot.LoadXml(_view.xmlEditor.Html);
                   xmlDoc.RemoveAll();
                   var firstChild =
                       editot.GetElementsByTagName("CodeSnippets",
                                                   "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet")[0];
                   var oNode = xmlDoc.CreateNode(firstChild.NodeType, firstChild.Name, firstChild.NamespaceURI);
                   oNode.InnerXml = (firstChild).InnerXml;
                   //necessary for crossing XmlDocument contexts
                   {
                       var importNode = xmlDoc.ImportNode(oNode, true);
                       xmlDoc.AppendChild(importNode);
                       xmlDoc.Save(SnippetPath);
                       MessageBox.Show("Snippet file saved successfully!", "Success", MessageBoxButton.OK,
                                          MessageBoxImage.Information);
                   }
               }
               else
               {
                   MessageBox.Show("Please enter vscontent path", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
               }

            });

        }


        private void BrowseVsCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(BrowseVs);
        }

      


        #endregion

        #region Methods

      

        private void BrowseVs()
        {
            var saveFileDialog = new SaveFileDialog
            {
                Filter = "Snippet (.snippet)|*.snippet"
            };
            saveFileDialog.ShowDialog();
            if (!string.IsNullOrEmpty(saveFileDialog.FileName))
            {
                SnippetPath = saveFileDialog.FileName;
                 var doc=new XmlDocument();
                   doc.Load(SnippetPath);
                _view.xmlEditor.Html = doc.InnerXml;
            }
        }

        #endregion

        #region Properties


        public string SnippetPath
        {
            get { return _snippetPath; }
            set
            {
                _snippetPath = value;
                NotifyPropertyChanged(this, o => o.SnippetPath);
            }
        }

        #endregion
    }
}
